<template>
  <!-- 搜索结果 -->
  <div class="resultView">
    <!-- 搜索框 -->
    <div class="search">
      <i class="iconfont icon-zuojiantou-copy" @click="$router.go(-1)"></i>
      <div class="center">
        <input type="text" placeholder="" @click="golist" :value="keyword" />
        <i class="iconfont icon-sousuo"></i>
      </div>
      <div class="right">搜索</div>
    </div>
    <!-- 回到顶部 -->
    <div class="bac-top" v-if="btnFlag" @click="backTop"></div>

    <!-- 列表数据 -->
    <div class="label">
      <div class="one" v-for="item in lists" :key="item.id">
        <router-link
          custom
          :to="'/details?id=' + item.id"
          v-slot="{ href, navigate }"
        >
          <div class="center" :href="href" @click="navigate">
            <div class="img">
              <img :src="item.coverImg" alt="" />
            </div>
            <div class="title">
              <h6>
                <span>{{ item.bookName }}</span>
                <span
                  ><i>{{ item.bookCategory.name }}</i></span
                >
              </h6>
              <div class="name">
                <span>{{ item.authorPenName }}</span>
              </div>
              <p>
                {{ item.introduction }}
              </p>
              <div class="txt">{{ item.bookStatus.name }}</div>
            </div>
          </div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { getsearchobj } from "../api/search";
import { onMounted, reactive, toRefs } from "vue";
import router from "@/router";
export default {
  props: ["keyword"],
  setup(props) {
    const list = reactive({
      lists: "",
      hradlist: [],

      btnFlag: false, //回到顶部默认不显示
      scrollTop: 200,
    });
    //请求数据
    let getsearcharr = () => {
      getsearchobj({
        num: 20,
        keyword: props.keyword,
        appKey: 4037465544,
        page: 1,
      }).then((data) => {
        console.log(data.data.lists);
        list.lists = data.data.lists;

        list.hradlist.push(list.lists[0]);
      });
    };
    //回到搜索
    let golist = () => {
      console.log(1);
      router.go(-1);
    };

    onMounted(() => {
      console.log(props, "111");
      getsearcharr();
    });

    // 回到顶部
    const backTop = () => {
      console.log(window, list.scrollTop, "top");

      let timer = setInterval(() => {
        let ispeed = Math.floor(-list.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          list.scrollTop + ispeed;
        if (list.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    };
    const scrollToTop = () => {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      list.scrollTop = scrollTop;
      if (list.scrollTop > 200) {
        list.btnFlag = true;
      } else {
        list.btnFlag = false;
      }
      // console.log(scrollTop, "top");
    };
    return {
      getsearcharr,
      golist,
      ...toRefs(list),
      backTop,
      scrollToTop,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
};
</script>

<style lang="scss" scoped>
.resultView {
  padding-top: 47px;
  background-color: #f5f5f5;
  .bac-top {
    position: fixed;
    bottom: 80px;
    right: 10px;
    width: 37px;
    height: 37px;
    background: url("../assets/img/10068.png") no-repeat center;
    background-size: 37px 37px;
  }
  .search {
    background-color: #fff;
    z-index: 10;
    position: fixed;
    border-bottom: 2px solid #f7f7f7;
    top: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    align-items: center;
    padding: 8px 15px;
    display: flex;
    justify-content: space-between;
    .center {
      width: 80%;
      position: relative;
      color: #b0b0b5;
      input {
        width: 100%;
        padding-left: 30px;
        box-sizing: border-box;
        font-size: 13px;
        border: none;
        background-color: #f6f6f6;
        border-radius: 8px;
        height: 30px;
      }
      i {
        position: absolute;
        left: 10px;
        top: 50%;
        font-size: 13px;
        transform: translateY(-50%);
      }
    }
  }
  .one {
    background-color: #fff;
    padding: 16px 21px;
    .center {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
      .img {
        margin-right: 12px;
        height: 107px;
        img {
          height: 100%;
        }
      }
      .title {
        padding-right: 1px;
        overflow: hidden;
        flex: 1;
        h6 {
          color: #000;
          display: flex;
          justify-content: space-between;
          width: 100%;
          font-size: 16px;

          i {
            color: #000;
            font-size: 12px;
          }
        }
        .name {
          margin: 6px 0;
          width: 70%;

          span {
            display: inline-block;
            background-color: #f8f8f8;
            color: #6c6c6c;
            font-size: 13px;
            padding: 2px 5px;
            border-radius: 5px;
          }
        }
        p {
          -webkit-line-clamp: 2;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 12px;
          color: #959595;
        }
        .txt {
          color: red;
          font-size: 12px;
          margin-top: 5px;
        }
      }
    }
    .bottom {
      margin: 3px 0;
      display: flex;
      justify-content: space-between;
      div {
        padding: 10px 50px;
        border-radius: 19px;
        font-size: 13px;
        border: 2px solid #dfdfdf;
        color: #0c0c0c;
        &:nth-child(2) {
          border-color: #00a167;
          background-color: #23b382;
          color: #fff;
        }
      }
    }
  }
  .ChiefEditor {
    margin: 10px 0;
    .box {
      background-color: #fff;
      padding: 10px 20px;
      h4 {
        box-sizing: border-box;
        width: 100%;
        font-size: 18px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        a {
          font-size: 12px;
          color: #333;
          i {
            font-size: 12px;
          }
        }
      }
      ul {
        display: flex;
        flex-wrap: wrap;
        li {
          margin-top: 19px;
          padding: 0 10px;
          width: 25%;
          box-sizing: border-box;
          .img {
            position: relative;
            img {
              width: 100%;
            }
            span {
              top: 8px;
              right: 4px;
              background-color: #ffe6ac;
              color: #5b3507;
              position: absolute;
              display: inline-block;
              font-size: 8px;
            }
          }
          h5 {
            font-size: 13px;
            margin-top: 5px;
          }
          p {
            margin: 3px;
            font-size: 11px;
            color: #939393;
          }
        }
      }
    }
  }
}
.label {
  margin-bottom: 100px;
}
</style>